package vn.com.horoscope.activity;

import vn.com.horoscope.R;
import vn.com.horoscope.common.ConstantClass;
import vn.com.horoscope.common.StaticClass;
import vn.com.horoscope.common.Utilities;
import vn.com.horoscope.entities.Zodiac;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class TodayHoroscopeActivity extends Activity {
	Zodiac zodiac ;
	private ImageView img ;
	private TextView txtName , txtToday ;
	private RatingBar rt1, rt2,rt3,rt4 ;
	private TextView txtDetail ;
	private Button btnShare ;
	private Button btnBack ;
	
	// Your Facebook APP ID
    private static String APP_ID = "545395775527409";
    private Facebook facebook;
	private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_today);
		init();
		loadData();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_today_horoscope, menu);
		return true;
	}

	
	private void init(){
		img = (ImageView) findViewById(R.id.imgTodayHoroscope);
		txtName = (TextView) findViewById(R.id.txtTodayHoroscopeName);
		txtToday = (TextView) findViewById(R.id.txtToday);
		
		rt1 = (RatingBar) findViewById(R.id.rtTinhYeu);
		rt2 = (RatingBar) findViewById(R.id.rtTaiChinh);
		rt3 = (RatingBar) findViewById(R.id.rtTamTrang);
		rt4 = (RatingBar) findViewById(R.id.rtSuNghiep);
		
		txtDetail = (TextView) findViewById(R.id.txtTodayDetail);
		btnShare = (Button) findViewById(R.id.btnShare) ;
		btnBack = (Button) findViewById(R.id.btnBackToDetail) ;
		
		facebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);
	}
	
	private void loadData() {
		int h =  (Integer) getIntent().getExtras().get(ConstantClass.horoscopeParam);
		zodiac = StaticClass.op.getData().getHoroscope().getZodiac().get(h-1);
		setImageHoroscope(h);
		txtName.setText(zodiac.getLabel());
		txtToday.setText(zodiac.getDate());
		txtDetail.setText(Utilities.removeString(zodiac.getContent(), ConstantClass.STAR_SPLIT));
		
		int[] val = Utilities.getValue(zodiac.getContent());
		rt1.setRating(val[0]);
		rt2.setRating(val[1]);
		rt3.setRating(val[2]);
		rt4.setRating(val[3]);
		
		btnShare.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				postToWallFacebook();
			}
		});
		
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				TodayHoroscopeActivity.this.finish();
			}
		});
		
	}
	
	/**
	 * 
	 */
	@SuppressWarnings("deprecation")
	private void postToWallFacebook(){
		    // post on user's wall.

				Bundle params = new Bundle();
			    params.putString("name", "Horo Daily - " + zodiac.getLabel() + " - "+ zodiac.getDate());
			    params.putString("caption", "https://daovietanh.net");
			    params.putString("description", " "+ Utilities.removeString(zodiac.getContent(), ConstantClass.STAR_SPLIT));
			    params.putString("link", "https://facebook.com/daovietanh90");
			    params.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
				
			    String access_token = facebook.getAccessToken();
			    if (access_token != null) {
			    	facebook.setAccessToken(access_token);
			    }

				
			    facebook.dialog(this, "feed", params, new DialogListener() {
			 
			        @Override
			        public void onFacebookError(FacebookError e) {
			        }
			 
			        @Override
			        public void onError(DialogError e) {
			        }
			 
			        @Override
			        public void onComplete(Bundle values) {
			        	Toast.makeText(TodayHoroscopeActivity.this, "�?ăng lên tư�?ng thành công !", Toast.LENGTH_LONG).show();
			        }
			 
			        @Override
			        public void onCancel() {
			        }
			        
			        
			    });
			
	}
	
	
	private void setImageHoroscope(int i) {
		switch (i) {
		case 1:
			img.setImageResource(R.drawable.bachduong);
			break;
		case 2:
			img.setImageResource(R.drawable.kimnguu);
			break;
		case 3:
			img.setImageResource(R.drawable.songtu);
			break;
		case 4:
			img.setImageResource(R.drawable.cugiai);
			break;
		case 5:
			img.setImageResource(R.drawable.sutu);
			break;
		case 6:
			img.setImageResource(R.drawable.xunu);
			break;
		case 7:
			img.setImageResource(R.drawable.thienbinh);
			break;
		case 8:
			img.setImageResource(R.drawable.bocap);
			break;
		case 9:
			img.setImageResource(R.drawable.nhanma);
			break;
		case 10:
			img.setImageResource(R.drawable.maket);
			break;
		case 11:
			img.setImageResource(R.drawable.baobinh);
			break;
		case 12:
			img.setImageResource(R.drawable.songngu);
			break;
		}
		
	}

	
	
		
}
